package com.gitee.cirnochat.user.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.user.model.vo.req.friend.FriendApplyAddRequest;
import com.gitee.cirnochat.user.model.vo.req.friend.FriendApplyInvitorAddRequest;
import com.gitee.cirnochat.user.model.vo.resp.FriendApplyVo;
import com.gitee.cirnochat.user.service.FriendApplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "好友请求相关接口", description = "好友请求相关API")
@RestController
@RequestMapping("/friend/apply")
@RequiredArgsConstructor
public class FriendApplyController {
    private final FriendApplyService friendApplyService;

    /**
     * 获取好友请求列表
     * @param request HttpServletRequest
     * @return 好友请求分页列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取好友请求列表", description = "分页获取好友请求列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = FriendApplyVo.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<FriendApplyVo>> listFriendRequests(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(friendApplyService.listFriendRequests(userId));
    }

/**
     * 获取好友请求总数
     * @param request HttpServletRequest
     * @return 好友请求总数
     */
    @GetMapping("/unread")
    @Operation(summary = "获取好友请求总数", description = "统计当前用户收到的好友请求总数")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Long> countFriendRequests(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(friendApplyService.countFriendRequests(userId));
    }

    //region 好友请求操作

    /**
     * 添加好友
     * @param req 好友请求信息
     * @param request HttpServletRequest
     * @return
     */
    @PostMapping("/send/")
    @Operation(summary = "通过qrcode扫描发送好友请求", description = "通过qrcode(id)发送好友请求")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> sendFriendRequestByQrCode(@Valid @RequestBody FriendApplyAddRequest req, HttpServletRequest request){
        Long userId = RequestUtils.getUserId(request);
        friendApplyService.sendFriendRequestById(req,userId);
        return Result.success();
    }

    /**
     * 推荐添加（INVITE）
     * @param dto 受荐好友请求信息
     * @param request HttpServletRequest
     * @return
     */
    @PostMapping("/send/invite")
    @Operation(summary = "通过好友推荐发送好友请求", description = "通过qrcode(id)发送好友请求")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> sendFriendRequestByInvited(@PathVariable FriendApplyInvitorAddRequest req, HttpServletRequest request){
        Long userId = RequestUtils.getUserId(request);
        friendApplyService.sendFriendRequestByInvited(req,userId);
        return Result.success();
    }



    /**
     * 同意好友请求
     * @param requestId 好友请求 ID
     * @param request HttpServletRequest
     * @return 是否成功
     */
    @PutMapping("/accept/{requestId}")
    @Operation(summary = "同意好友请求", description = "同意好友请求")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> acceptFriendRequest(@Parameter(description = "好友请求 ID", required = true , in = ParameterIn.PATH) @PathVariable Long requestId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        friendApplyService.acceptFriendRequest(requestId, userId);
        return Result.success();
    }

    /**
     * 拒绝好友请求
     * @param requestId 好友请求 ID
     * @param request HttpServletRequest
     * @return 是否成功
     */
    @PutMapping("/reject/{requestId}")
    @Operation(summary = "拒绝好友请求", description = "拒绝好友请求")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> rejectFriendRequest(@Parameter(description = "好友请求 ID", required = true , in = ParameterIn.PATH) @PathVariable Long requestId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        friendApplyService.rejectFriendRequest(requestId, userId);
        return Result.success();
    }

    /**
     * 撤回/删除(当添加成功后)好友请求
     * @param requestId 好友请求 ID
     * @param request HttpServletRequest
     * @return 是否成功
     */
    @DeleteMapping("/revoke/{requestId}")
    @Operation(summary = "撤回好友请求", description = "撤回/删除(当添加成功后)好友请求")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> revokeFriendRequest(@Parameter(description = "好友请求 ID", required = true , in = ParameterIn.PATH) @PathVariable Long requestId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        friendApplyService.revokeFriendRequest(requestId, userId);
        return Result.success();
    }

    //endregion

}
